<?php

namespace Modules\Order\Models;

use Catch\Base\CatchModel as Model;
use Catch\Traits\DB\BaseOperate;
use Catch\Traits\DB\ScopeTrait;
use Catch\Traits\DB\Trans;
use Catch\Traits\DB\WithAttributes;
use Illuminate\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Laravel\Sanctum\HasApiTokens;
use Modules\Member\Models\Members;
use Modules\Shop\Models\Coupon;

class Orders extends Model implements AuthenticatableContract
{
    use Authenticatable, HasApiTokens;

    protected $table = 'order';
    public $timestamps = true;
//    public $appends = ['overweight_paid_status'];
    protected $fillable = [
        'id',
        'user_id',
        'waybill',
        'package_count',
        'receiver',
        'receiver_mobile',
        'receive_province',
        'sender_province',
        'sender_city',
        'receive_city',
        'receive_county',
        'receive_town',
        'receive_location',
        'weight',
        'insured',
        'vloum_long',
        'vloum_width',
        'vloum_height',
        'order_sn',
        'money',
        'cancel_remark',
        'type_code',
        'status',
        'created_at',
        'updated_at',
        'deleted_at',
        'paid_at',
        'freight',
        'is_overweight',
        'overweight_sn',
        'overweight_paid_at',
        'overweight_at',
        'is_haocai',
        'haocai_sn',
        'haocai_money',
        'haocai_paid_at',
        'haocai_refund_sn',
        'haocai_at',
        'real_price',
        'overweight_money',
        'tag_type',
        'postman_name',
        'postman_mobile',
        'item_name',
        'type',
        'cal_weight',
        'real_weight',
        'receive_address',
        'sender_address',
        'coupon_id',
        'bill_remark',
        'sender_mobile',
        'volume',
        'price_one',
        'price_more',
        'fee_type',
        'tag_str',
        'order_refund_sn',
        'channel_type',
    ];
    public array $searchable = [
        'id' => '=',
        'user_id' => '=',
        'order_sn' => '=',
        'waybill' => '=',
        'sender_mobile' => '=',
        'status' => '=',
        'type_code' => '=',
        'is_overweight' => '=',
        'is_haocai' => '=',


    ];

    public function getPaidAtAttribute($value)
    {
        if(!empty($value)){
            return date("Y-m-d H:i", $value);
        }else{
            return $value;
        }


    }

    public function getOverweightPaidAtAttribute($value)
    {
        if(!empty($value)){
            return date("Y-m-d H:i", $value);
        }else{
            return $value;
        }


    }

    public function getHaocaiPaidAtAttribute($value)
    {
        if(!empty($value)){
            return date("Y-m-d H:i", $value);
        }else{
            return $value;
        }


    }

    public function getRefundStatusAttribute()
    {
        if(!empty($this->attributes['order_refund_sn'])){
            return 1;
        }else{
            return 0;
        }


    }

    public function user()
    {
        return $this->belongsTo(Members::class, 'user_id')->select(['id', 'avatar', 'username', 'mobile']);
    }

    public function coupon()
    {
        return $this->belongsTo(Coupon::class, 'coupon_id');
    }

    public function getOverweightPaidStatusAttribute()
    {
        if($this->attributes['is_overweight'] ==0){
            return '';
        }elseif($this->attributes['is_overweight'] == 1 && $this->attributes['overweight_paid_at'] == 0){
            return 0;
        }else{
            return 1;
        }
    }
}
